package tagdb.algorithm.graph;

import tagdb.fragmentation.SpectrumPeak;

/**
 *  The Edge class represents a weighted edge in an undirected graph.
 *  @author Thilo Muth
 */
public class Edge implements Comparable<Edge> {

    // The first node
    private SpectrumPeak firstPeak;

    // The second node (other end for the edge)
    private SpectrumPeak secondPeak;

    // The weight for the edge
    private double weight;

    /**
     * Create an edge between firstPeak and secondPeak with given weight of the m/z difference.
     */
    public Edge(SpectrumPeak firstPeak, SpectrumPeak secondPeak) {
        this.firstPeak = firstPeak;
        this.secondPeak = secondPeak;
        this.weight = Math.abs(firstPeak.getMZ() - secondPeak.getMZ());
    }

    /**
     * Returns the weight of this edge.
     */
    public double getWeight() {
        return weight;
    }

    /**
     * Returns the end of this edge that is different from the given vertex.
     */
    public SpectrumPeak getOther(SpectrumPeak peak) {
        if      (peak == firstPeak) return secondPeak;
        else if (peak == secondPeak) return firstPeak;
        else return null;
    }

    /**
     * Compares edges by weight.
     */
    public int compareTo(Edge edge) {
        if (this.getWeight() < edge.getWeight()) return -1;
        else if (this.getWeight() > edge.getWeight()) return +1;
        else return 0;
    }

    public SpectrumPeak getFirstPeak() {
        return firstPeak;
    }

    public SpectrumPeak getSecondPeak() {
        return secondPeak;
    }
}

